<?php

namespace App\Http\Controllers\Api\V1;

use Carbon\Carbon;
use App\Models\ActivityRecord;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Api\V1\Resources\ActivityRecordResource;

class ActivitiesRecordsController extends Controller
{
	public function index()
	{
		$type = request('type');
		if($type==1){
			$startAt = Carbon::now()->startOfMonth()->toDateTimeString();
			$endAt = Carbon::now()->endOfMonth()->toDateTimeString();
		}else{
			$firstOfMonth = new Carbon('first day of last month');
			$startAt = $firstOfMonth->startOfMonth()->toDateTimeString();

			$lastOfMonth = new Carbon('last day of last month');
    		$endAt = $lastOfMonth->endOfMonth()->toDateTimeString();

		}

		$records = ActivityRecord::query()
			->select("id","user_id",DB::raw('SUM(money) as money'))
			->whereBetween('created_at', [$startAt, $endAt])
			->groupBy("user_id")
			->orderByDesc("money")
			->paginate();
		
		return ActivityRecordResource::collection($records);
	}

	
}
